<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    die('Unauthorized');
}
require_once 'db.php';

$content = $_POST['content'] ?? '';
if ($content === '') die('No content provided');

// 不做任何过滤，直接保存
$stmt = $pdo->query('SELECT id FROM knowledge_base LIMIT 1');
$row = $stmt->fetch();
if ($row) {
    $stmt = $pdo->prepare('UPDATE knowledge_base SET content=?, updated_at=CURRENT_TIMESTAMP WHERE id=?');
    $stmt->execute([$content, $row['id']]);
} else {
    $stmt = $pdo->prepare('INSERT INTO knowledge_base (content) VALUES (?)');
    $stmt->execute([$content]);
}
header('Location: index.php?success=1');
exit;
?> 